package net.xiaoxiangshop.controller.member;

import javax.inject.Inject;

import net.xiaoxiangshop.service.SmsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.xiaoxiangshop.Results;
import net.xiaoxiangshop.entity.Member;
import net.xiaoxiangshop.security.CurrentUser;
import net.xiaoxiangshop.service.MemberService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller - 密码
 * 
 */
@Controller("memberPasswordController")
@RequestMapping("/member/password")
public class PasswordController extends BaseController {

	@Inject
	private MemberService memberService;
	@Inject
	private SmsService smsService;
	/**
	 * 验证当前密码
	 */
	@GetMapping("/check_current_password")
	public @ResponseBody boolean checkCurrentPassword(String currentPassword, @CurrentUser Member currentUser) {
		return StringUtils.isNotEmpty(currentPassword) && currentUser.isValidCredentials(currentPassword);
	}

	/**
	 * 编辑
	 */
	@GetMapping("/edit")
	public String edit() {
		return "member/password/edit";
	}

	/**
	 * 更新
	 */
	@PostMapping("/update")
	public ResponseEntity<?> update(String currentPassword, String password, @CurrentUser Member currentUser) {
//		if (StringUtils.isEmpty(password) || StringUtils.isEmpty(currentPassword)) {
//			return Results.UNPROCESSABLE_ENTITY;
//		}
		if (StringUtils.isEmpty(password)) {
			return Results.UNPROCESSABLE_ENTITY;
		}
		if (!isValid(Member.class, "password", password)) {
			return Results.UNPROCESSABLE_ENTITY;
		}
//		if (!currentUser.isValidCredentials(currentPassword)) {
//			return Results.UNPROCESSABLE_ENTITY;
//		}

		currentUser.setPassword(password);
		memberService.update(currentUser);
		return Results.OK;
	}


	/**
	 * business登陆获取短信验证码
	 * @param currentUser
	 * @return
	 */
	@GetMapping("/getSmsCode")
	public ResponseEntity<?> getSmsCode(@CurrentUser Member currentUser) {
		//判断手机号是存在
		String mobile=currentUser.getMobile();
		if(!StringUtils.isEmpty(mobile)){
			smsService.sendSmsCode("2",mobile);
			return responseReslut(200,"短信验证已发送");
		}else{
			return responseReslut(100,"验证码发送失败");
		}
	}

	/**
	 * 校验短信验证码
	 * @param currentUser
	 * @param smsCode
	 * @return
	 */
	@PostMapping("/checkSmsCode")
	public ResponseEntity<?> checkSmsCode(String smsCode,@CurrentUser Member currentUser) {
		String mobile=currentUser.getMobile();
		if(StringUtils.isEmpty(mobile)){
			return responseReslut(100,"短信验证码校验失败");
		}
		ResponseEntity<?> reslut=smsService.checkSmsCode("2",mobile,smsCode);
		Integer code= reslut.getStatusCodeValue();
		if(code==200){//验证码通过、
			return responseReslut(200,"短信验证码校验成功");
		}
		return responseReslut(100,"短信验证码校验失败");
	}

	public ResponseEntity<?> responseReslut(Integer code,String message){
		Map<String,Object> responseReslut=new HashMap<>();
		responseReslut.put("code",code);
		responseReslut.put("message",message);
		return ResponseEntity.ok(responseReslut);
	}

}